<?php defined('SYSPATH') or die('No direct access allowed.');

class Model_Api_Standard_System
{

    public static function get_model_type_list()
    {
        $sql = "SELECT
	id,
	modulename,
	pinyin,
	issystem,
	isopen
FROM
	sline_model";

        return DB::query(Database::SELECT, $sql)->execute()->as_array();

    }

    /*
     * 将相对文件url换成绝对文件url
     */
    public static function reorganized_resource_url($url)
    {
       // return (stripos(strtolower($url), "http://") !== 0 ? (empty($url) ? "" : "{$GLOBALS['cfg_basehost']}/{$url}") : $url);
	   if(!empty($url))
	   {
			$http = stripos(strtolower($url), 'http://') ;
			$https = stripos(strtolower($url), 'https://') ;
			if( $http === $https)
			{
			   return  $GLOBALS['cfg_basehost'] .'/'. $url;
			}
	   }
	   return $url;
    }

    /*
     * 将unix时间戳换成可识别时间字符串
     */
    public static function format_unixtime($unix_time)
    {
        return ($unix_time ? date("Y-m-d H:i:s", $unix_time) : "");
    }

    /**
     * 获取系统参数值
     */
    public static function get_sys_para($varname)
    {
         $v = DB::select('value')->from('sysconfig')->where('varname','=',$varname)->and_where('webid','=',0)->execute()->get('value');

         return  $v ? $v : '';
    }

    /**
     * 获取分销配置参数值
     */
    public static function get_fenxiao_para($varname)
    {
        if($varname=='cfg_wxxcx_appid')
        {
            $appkey = Model_Sysconfig::get_sys_conf('value', 'cfg_global_client_appkey');
            if($appkey)
            {
                return $appkey;
            }
        }
        if($varname=='cfg_wxxcx_appsecret')
        {
            $appsecret = Model_Sysconfig::get_sys_conf('value', 'cfg_global_client_appsecret');
            if($appsecret)
            {
                return $appsecret;
            }
        }
        $v = DB::select('value')->from('fenxiao_config')->where('varname','=',$varname)->execute()->get('value');

        return  $v ? $v : '';
    }
    /**
     * @function 获取星期
     * @param $day
     * @return mixed
     */
    public static function get_weekday($day)
    {
        $w = date('w',$day);
        //注意上面返回的都是 数字,0123456.所以如果要显示中文的星期，可以定义下面的数组就可以了。
        $weekarray=array("日","一","二","三","四","五","六"); //0表示星期日
        return $weekarray[$w];
    }


    /**
     * @function 获取产品的保险信息
     * @param $id 产品ID
     * @param $typeid  产品类型ID
     * @return mixed
     */
    public static function get_insurance($id,$typeid)
    {
        $list = array();
        $planlist = DB::select('planlist')->from('insurance_product_list')->where('typeid','=',$typeid)->and_where('productid','=',$id)->execute()->get('planlist');
        if($planlist)
        {
            $sql = "SELECT a.id,a.baseid,a.title,a.price,c.ageword,d.lowerday,d.highday,e.title as base_title from sline_insurance_plan a ";
            $sql .= " LEFT JOIN sline_insurance_age c ON (a.ageid = c.id) LEFT JOIN sline_insurance_expires d ON (a.expiresid = d.id) ";
            $sql .= " LEFT JOIN sline_insurance_base e ON a.baseid = e.id ";
            $sql .= " WHERE a.id in({$planlist}) ORDER BY a.id asc ";
            $list = DB::query(Database::SELECT,$sql)->execute()->as_array();
            foreach($list as &$v)
            {
                //获取保障项目
                $plan_project_arr = DB::select()->from('insurance_plan_project')->where('planid','=',$v['id'])->and_where('title','!=','请输入项目名称')->execute()->as_array();
                foreach($plan_project_arr as $k=>$row)
                {
                    //$plan_project_title[] = $row['title'];
                    $items_arr[] = DB::select()->from('insurance_plan_items')->where('projectid','=',$row['id'])->execute()->as_array();
                }
                //$v['plan_project_title'] = $plan_project_title;
                $v['items_arr'] = $items_arr;
            }
        }

        return $list;
    }

    /**
     * 栏目获取首页类型
     * @param $typeid
     * @return mixed
     */
    public static function get_index_attr($typeid)
    {
        if($typeid == 1)
        {
            $table = 'line_attr';
        }
        elseif ($typeid == 2)
        {
            $table = 'hotel_attr';
        }
        elseif ($typeid == 3)
        {
            $table = 'car_attr';
        }
        elseif ($typeid == 5)
        {
            $table = 'spot_attr';
        }
        $id = DB::select('id')->from($table)->where('pid','=','0')
              ->and_where('isopen','=','1')->limit(1)->execute()->get('id');

        $arr = DB::select('id','pid','attrname')->from($table)->where('pid','=',$id)->and_where('isopen','=','1')
            ->order_by(DB::expr('ifnull(displayorder,9999)'),'asc')->limit(9)->execute()->as_array();

        return $arr;
    }

    /**
     * @function 获取小程序权限
     * @param $mini_type 1 三方平台小程序(需要检查是否是组合小程序) 2 独立小程序
     * @param $siteId
     * @param $secret
     * @return array
     */
    public static function get_xcx_auth($mini_type,$siteId,$secret)
    {
        $tag_type  = 1; // 1 综合版本 2 组合小程序
        if($mini_type == 1)
        {
            // 检查小程序是否是组合小程序并进行权限控制
            $mini_auth = Model_Api_Standard_App::get_app_auth($siteId,$secret);
            if(isset($mini_auth['appInfo']) && $mini_auth['appInfo'])
            {
                $tag_type = $mini_auth['appInfo']['type'];
            }
        }

        $result = array();
        if($tag_type == 2)
        {
            $auth = $mini_auth['appInfo']['auth'];
            foreach ($auth as $v)
            {
                array_push($result, $v['update_ProductCode']);
            }
        }
        else
        {
            $product = DB::select('productcode')->from('app')->where('system_part_type', '=', 2)->execute()->as_array();
            foreach ($product as $item)
            {
                if (strpos($item['productcode'], 'stourwebcms_app_')!==false)
                {
                    array_push($result, str_replace('stourwebcms_app_', '', $item['productcode']));
                }
            }
        }

        return $result;
    }

    /**
     * @function 通过appCode获取产品typeid 主要用于首页产品展示控制
     * @param $data
     * @return array
     */
    public static function get_xcx_product_typeid($data)
    {
        $p_arr = array(
            "system_line"=> 1,
            "system_hotel"=> 2,
            "system_car"=> 3,
            "system_article"=> 4,
            "system_spot"=> 5,
            "system_visa"=> 8,
            "system_customize"=> 14,
            "system_ship_line"=> 104,
            "system_guide"=> 106,
            "system_notes"=> 101,
            "system_outdoor"=> 114,
            "system_techan"=> 118
        );

        $res = array();

        foreach ($data as $v)
        {
            if(isset($p_arr[$v]))
            {
                $res[] = $p_arr[$v];
            }
        }

        // 检查导航控制 暂时使用移动端主要航进行控制首页产品展示
        $nav = DB::select()->from('m_nav')->where('m_isopen','=','1')->order_by('m_displayorder','asc')->execute()->as_array();
        $new_res = array();
        foreach ($nav as $v)
        {
            if(in_array($v['m_typeid'],$res))
            {
                $new_res[] = intval($v['m_typeid']);
            }
        }
        return $new_res;
    }

    /**
     * @function 通过typeid获取url
     * @param $typeid
     * @return string
     */
    public static function get_product_detail_path($typeid)
    {
        $url = "";
        if($typeid == 1)
        {
            $url = '/stpage/lines/pages/lineShow/lineShow';
        }
        elseif($typeid == 2)
        {
            $url = '/stpage/hotels/pages/hotelShow/hotelShow';
        }
        elseif($typeid == 3)
        {
            $url = '/stpage/cars/pages/carShow/carShow';
        }
        elseif($typeid == 5)
        {
            $url = '/stpage/spots/pages/mpShow/mpShow';
        }
        elseif($typeid == 4)
        {
            $url = '/stpage/article/pages/show/show';
        }
        elseif($typeid == 8)
        {
            $url = '/stpage/visa/pages/visaDetail/visaDetail';
        }
        elseif($typeid == 101)
        {
            $url = '/stpage/travelNotes/pages/details/details';
        }
        elseif($typeid == 104)
        {
            $url = '/stpage/ship/pages/show/show';
        }
        elseif($typeid == 106)
        {
            $url = '/stpage/guide/pages/guideServiceDetail/guideServiceDetail';
        }
        elseif($typeid == 114)
        {
            $url = '/stpage/outdoor/pages/details/details';
        }
        elseif($typeid == 118)
        {
            $url = '/stpage/techan/pages/show/show';
        }

        return $url;
    }

}